import React, { Component } from 'react'
import Form from './components/Form'
import List from './components/List'
import Tabs from './components/Tabs'


export default class App extends Component {
  state = {
    tabs: [
      {
        id: 1,
        name: '热度',
        type: 'hot',
      },
      {
        id: 2,
        name: '时间',
        type: 'time',
      },
    ],
    active: 'time',
    list: [
      {
        id: 1,
        author: '刘德华',
        comment: '给我一杯忘情水',
        time: new Date('2021-10-10 09:09:00'),
        img: 'https://y.qq.com/music/photo_new/T001R300x300M000003aQYLo2x8izP.jpg?max_age=2592000',
        // 1: 点赞 0：无态度 -1:踩
        attitude: 1,
      },
      {
        id: 2,
        author: '周杰伦',
        comment: '听妈妈的话',
        time: new Date('2021-10-11 08:09:00'),
        img: 'https://y.qq.com/music/photo_new/T001R500x500M0000025NhlN2yWrP4.jpg?max_age=2592000',
        // 1: 点赞 0：无态度 -1:踩
        attitude: 0,
      },
      {
        id: 3,
        author: '陈奕迅',
        comment: '十年',
        time: new Date('2021-10-11 09:09:00'),
        img: 'https://y.qq.com/music/photo_new/T001R500x500M000003Nz2So3XXYek.jpg?max_age=2592000',
        // 1: 点赞 0：无态度 -1:踩
        attitude: -1,
      },
    ],
  }
  // 排序
  handleActive = (active) => {
    this.setState({ active })
  }
  // 添加
  // 父组件提供 添加的方法
  handleAdd = (comment) => {
    this.setState({
      list: [comment, ...this.state.list]
    })
  }
  // 删除
  handleDelete = (id) => {
    this.setState({
      list: this.state.list.filter(item => item.id !== id)
    })
  }
  // 点赞
  handleZan = (id, attitude) => {
    this.setState({
      list: this.state.list.map(item => {
        if (item.id === id) {
          return {
            ...item,
            attitude
          }
        } else {
          return item
        }
      })
    })
  }
  render () {
    const { tabs, active, list } = this.state
    return (
      <div className='App'>
        <div className='comment-container'>
          <div className='comment-head'>
            <span>{list.length}评论</span>
          </div>
          {/* Tabs */}
          <Tabs tabs={tabs} active={active} handleActive={this.handleActive} />
          {/* Form */}
          <Form handleAdd={this.handleAdd} />
          {/* List */}
          <List list={list} active={active}
            handleDelete={this.handleDelete}
            handleZan={this.handleZan} />
        </div>
      </div>
    )
  }
}